# coding: utf-8
module DocHelper

	def getCateName(cateid, cates)
		cates.each do |cate|
			return cate.name if cateid == cate.id
		end
		""
	end

	# 获取类别链接样式
	def getCateLinkClass(key)
		url = request.url
		action = self.action_name
		if(action == 'index')
			return 'current' if(url.include? key)
			return 'current' if(key == 'list/all' and params[:cateid] == nil)
		end
		if(action == 'new')
			return 'current' if(key == 'new')
		end
		''
	end

	def getCateClass(key)
		url = request.url << '/'
		action = self.action_name
		if(action == 'index')
			return 'active' if(url.include? key)
			return 'active' if(key == 'list/all' and params[:cateid] == nil)
		end
		if(action == 'new')
			return 'active' if(key == 'new')
		end
		''
	end

	# 获取随机的共享文档
	def getRandDoc(num)
		DocMain.select('id, userid, title, shareid, updated_at').where(:shareid => 1, :stateid => 1).all(:order => "id desc", :limit => num * 2).sample(num)
	end

	# 格式化日期
	def formatDate(updated_at)
		current = updated_at.in_time_zone(+8.hours).strftime('%Y-%m-%d')
		today = Time.now.in_time_zone(+8.hours).strftime('%Y-%m-%d')
		yestoday = (Time.now - 1.day).in_time_zone(+8.hours).strftime('%Y-%m-%d')
		beforeyestoday = (Time.now - 2.day).in_time_zone(+8.hours).strftime('%Y-%m-%d')
		return '今天' if(current == today)
		return '昨天' if(current == yestoday)
		return '前天' if(current == beforeyestoday)
		current
	end

end
